import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions} from '@angular/http';
import {adSystemApiUrl, appletApiUrl} from '../../../config/api';
import {HttpService} from './http.service';

@Injectable()
export class LoginService {

  constructor(private http: Http, private httpService: HttpService) {

  }

  doLogin(username, password) {
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({mobile: username, password: password});
    return this.http.post(appletApiUrl + '/backend/applet_member/login', body, options).map(response => {
      const resdata = response.json();
      if (resdata.data !== "") {
        resdata.data.user = {};
        resdata.data.user.nick_name = username;
        // resdata.data.user.time = "" ;
      }
      return resdata;
    });
  }

  register(redirectMsg) {
    const url = adSystemApiUrl + '/v1/member/auth/register';
    return this.httpService.httpPostAuth(url, redirectMsg).map(data => data);
  }

  smsSend(mobile: number, type: number) {
    const url = adSystemApiUrl + '/v1/common/sms/send';
    return this.httpService.httpPostAuth(url, {mobile: mobile, type: type}).map(data => data);
  }

  getTableId() {
    const url = adSystemApiUrl + '/v1/common/captcha/get_table_id';
    return this.httpService.httpGetAuth(url).map(data => data);
  }

  checkCode(code, table_id) {
    const url = adSystemApiUrl + `/v1/common/captcha/api_check?code=${ code }&table_id=${ table_id }`;
    return this.httpService.httpGetAuth(url).map(data => data);
  }

  passedit(passeditMsg) {
    const url = adSystemApiUrl + '/v1/member/auth/passedit';
    return this.httpService.httpPostAuth(url, passeditMsg).map(data => data);
  }

  doLoginAdmin(account, password) {
    const url = adSystemApiUrl + '/v1/admin/auth/login';
    return this.httpService.httpPostAuth(url, {account: account, password: password}).map(data => data);
  }
}
